﻿namespace JshRCSWebApi.API.Utility.JwtConfig
{
    public class JwtManager
    {
        private readonly IConfiguration _configuration;
        private readonly JwtSecurityTokenHandler _jwtSecurityTokenHandler;
        public JwtManager(IConfiguration configuration, JwtSecurityTokenHandler jwtSecurityTokenHandler)
        {
            _configuration = configuration;
            _jwtSecurityTokenHandler = jwtSecurityTokenHandler;
        }

        /// <summary>
        /// 创建加密JwtToken
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public string CreateJwtToken<T>(T user)
        {
            var signingAlogorithm = SecurityAlgorithms.HmacSha256;
            var claimList = CreateClaimList(user);
            string Secret = SysConfig.Params.Project.JwtConfig.Secret;
            string Iss = SysConfig.Params.Project.JwtConfig.Iss;
            string Aud = SysConfig.Params.Project.JwtConfig.Aud;
            //Signature
            var secretByte = Encoding.UTF8.GetBytes(Secret);//取出私钥并以utf8编码字节输出
            var signingKey = new SymmetricSecurityKey(secretByte);//使用非对称算法对私钥进行加密
            var signingCredentials = new SigningCredentials(signingKey, signingAlogorithm);//使用HmacSha256来验证加密后的私钥生成数字签名
            //生成Token
            var Token = new JwtSecurityToken(
                issuer: Iss, //发布者
                audience: Aud, //接收者
                claims: claimList, //存放的用户信息
                notBefore: DateTime.UtcNow, //发布时间
                expires: DateTime.UtcNow.AddHours(24), //有效期设置为24小时
                signingCredentials //数字签名
            );
            //生成字符串token
            var TokenStr = new JwtSecurityTokenHandler().WriteToken(Token);
            return TokenStr;
        }



        public T GetToken<T>(string Token)
        {
            Type t = typeof(T);

            object objA = Activator.CreateInstance(t);
            var b = _jwtSecurityTokenHandler.ReadJwtToken(Token);
            foreach (var item in b.Claims)
            {
                PropertyInfo _Property = t.GetProperty(item.Type);
                if (_Property != null && _Property.CanRead)
                {
                    _Property.SetValue(objA, item.Value, null);
                }

            }
            return (T)objA;
        }

        /// <summary>
        /// 创建包含用户信息的CalimList
        /// </summary>
        /// <param name="authUser"></param>
        /// <returns></returns>
        private List<Claim> CreateClaimList<T>(T authUser)
        {
            List<Claim> claimList = new List<Claim>();
            try
            {
                var Class = typeof(T);
                foreach (var item in Class.GetProperties())
                {
                    claimList.Add(new Claim(item.Name, item.GetValue(authUser)?.ToString()));
                }
            }
            catch (Exception ex)
            {
                throw;
            }

            //claimList.Add(new Claim("ID", authUser.ToString()));
            return claimList;
        }



    }

    /// <summary>
    /// 用户注册
    /// </summary>
    public class User
    {
        /// <summary>
        /// 用户编号
        /// </summary>
        public long ID { get; set; }

        /// <summary>
        /// 用户名称
        /// </summary>
        public string Name { get; set; } = string.Empty;

    }

}
